#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("鱼群检测");
    this->setFixedSize(1280,720);
//    this->setWindowFlags(Qt::FramelessWindowHint);

//################################################
//#                                              #
//#           设置主界面背景                       #
//#           最后一次修改4.9，11：16               #
//#                                              #
//################################################
    QPixmap widget_background = QPixmap(":/ocean.jpg");
    widget_background = widget_background.scaled(1280,720,Qt::KeepAspectRatio);
    QPixmap temp_background(widget_background.size());
    temp_background.fill(Qt::transparent);
    //白色透明度
    QPainter picture_background(&temp_background);
    picture_background.setCompositionMode(QPainter::CompositionMode_Source);
    picture_background.drawPixmap(0,0,widget_background);
    picture_background.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    picture_background.fillRect(widget_background.rect(),QColor(0,0,0,255));
    picture_background.end();

    widget_background = temp_background;
    //黑色透明度
    //    QPainter picture_background(&widget_background);
    //    picture_background.setCompositionMode(QPainter::CompositionMode_Source);
    //    picture_background.drawPixmap(0,0,widget_background);
    //    picture_background.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    //    picture_background.fillRect(widget_background.rect(),QColor(0,0,0,150));
    //    picture_background.end();

    ui->label_background->setPixmap(widget_background);
    //调色板：使用失败
    //QPalette pal = this->palette();
    //QPixmap widget_background = QPixmap(":/ocean.jpg");
    //widget_background = widget_background.scaled(1280,720,Qt::KeepAspectRatio);
    //pal.setBrush(QPalette::Window,QBrush(widget_background));
    //setPalette(pal);

//################################################
//#                                              #
//#           窗口切换相关                         #
//#           最后一次修改4.9，7：51                #
//#                                              #
//################################################
//页面切换，不知道为什么要从 page2 开始
    ui->stackedWidget->setFixedSize(1280,720);
    p1 = new page1;
    p2 = new page2;
    p3 = new page3;
    p4 = new page4;
    ui->stackedWidget->addWidget(p1);
    ui->stackedWidget->addWidget(p2);
    ui->stackedWidget->addWidget(p3);
    ui->stackedWidget->addWidget(p4);
//    ui->stackedWidget->setCurrentWidget(0);
//界面切换按钮的信号和槽连接
    //page_main
    connect(this,&Widget::pushbutton_main_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p1);
        //qDebug()<<"test"<<"\n";
    });
    //page1
    connect(p1,&page1::pushbutton_page1_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p1);
    });
    connect(p1,&page1::pushbutton_page2_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p2);
    });
    connect(p1,&page1::pushbutton_page3_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p3);
    });
    connect(p1,&page1::pushbutton_page4_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p4);
    });
    //page2
    connect(p2,&page2::pushbutton_page1_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p1);
    });
    connect(p2,&page2::pushbutton_page2_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p2);
    });
    connect(p2,&page2::pushbutton_page3_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p3);
    });
    connect(p2,&page2::pushbutton_page4_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p4);
    });
    //page3
    connect(p3,&page3::pushbutton_page1_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p1);
    });
    connect(p3,&page3::pushbutton_page2_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p2);
    });
    connect(p3,&page3::pushbutton_page3_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p3);
    });
    connect(p3,&page3::pushbutton_page4_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p4);
    });
    //page4
    connect(p4,&page4::pushbutton_page1_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p1);
    });
    connect(p4,&page4::pushbutton_page2_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p2);
    });
    connect(p4,&page4::pushbutton_page3_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p3);
    });
    connect(p4,&page4::pushbutton_page4_clicked,this,[=](){
        ui->stackedWidget->setCurrentWidget(p4);
    });

//################################################
//#                                              #
//#           udp视频推流相关                      #
//#           最后一次修改4.9，11：52               #
//#                                              #
//################################################
    connect(p1,&page1::reload_frame,this,&Widget::slot_pass_frame);


//################################################
//#                                              #
//#           数据共享相关初始化                    #
//#           最后一次修改4.14，12：52              #
//#                                              #
//################################################
    data_upup_main = new Data_upup;
    p1->data_upup = this->data_upup_main;
    p2->data_upup = this->data_upup_main;
    p4->data_upup = this->data_upup_main;
    //串口连接信号和槽函数
    connect(data_upup_main,&Data_upup::signal_send_JiXieBi_arm,p2,&page2::Data_Send_upup);
    connect(data_upup_main,&Data_upup::signal_send_JiXieBi_hand,p2,&page2::Data_Send_upup);
    connect(data_upup_main,&Data_upup::signal_send_Light,p2,&page2::Data_Send_upup);
    connect(data_upup_main,&Data_upup::signal_send_Pid,p2,&page2::Data_Send_upup);
    connect(data_upup_main,&Data_upup::signal_send_Sport,p2,&page2::Data_Send_upup);
    connect(data_upup_main,&Data_upup::signal_send_YunTai,p2,&page2::Data_Send_upup);
    //tcp服务器端连接信号和槽函数
    connect(data_upup_main,&Data_upup::signal_send_JiXieBi_arm,p3,&page3::send_data_from_client);
    connect(data_upup_main,&Data_upup::signal_send_JiXieBi_hand,p3,&page3::send_data_from_client);
    connect(data_upup_main,&Data_upup::signal_send_Light,p3,&page3::send_data_from_client);
    connect(data_upup_main,&Data_upup::signal_send_Pid,p3,&page3::send_data_from_client);
    connect(data_upup_main,&Data_upup::signal_send_Sport,p3,&page3::send_data_from_client);
    connect(data_upup_main,&Data_upup::signal_send_YunTai,p3,&page3::send_data_from_client);

//################################################
//#                                              #
//#           手柄相关初始化                       #
//#           最后一次修改4.9,11：52               #
//#                                              #
//################################################
    game_pader = new shoubing;
    game_pader->p1_in_gamepad = this->p1;

}

Widget::~Widget()
{
    delete ui;
}

//################################################
//#                                              #
//#           窗口切换相关函数                      #
//#           最后一次修改4.9，7：51                #
//#                                              #
//################################################
void Widget::on_pushButton_main_clicked()
{
    emit pushbutton_main_clicked();
//    QPalette pal = this->palette();
//    pal.setBrush(QPalette::Shadow,QBrush(Qt::white));
//    setPalette(pal);
    ui->frame->close();
}

//################################################
//#                                              #
//#           udp视频推流相关                      #
//#           最后一次修改4.9，19：59               #
//#                                              #
//################################################

void Widget::slot_pass_frame()
{
    pass_frame(p1,p3);
    return;
}

void Widget::pass_frame(page1 * p1_pass_frame,page3 * p3_pass_frame)
{
    this->frame_widget = p1_pass_frame->read_now_frame();
    p3_pass_frame->reload_frame_push(frame_widget);
    return;
}


